/*
 * LayoutGrid.java
 *
 *
 */

package com.gpsnav.ui;

import net.rim.device.api.ui.*;
import com.gpsnav.bo.Options;

public class LayoutGrid extends Manager
{
    private int cols, layoutWidth, layoutHeight;
    private int[] colsWidth;
    
    
    public LayoutGrid(int nCols)
    {
        //construct a manager with vertical scrolling
        super(0);
        cols = nCols;
        colsWidth = new int[cols];
    }
    
    public LayoutGrid(int nCols, long layout)
    {
        //construct a manager with vertical scrolling
        super(layout);
        cols = nCols;
        colsWidth = new int[cols];
    }
    
    //overwrite the nextFocus method for custom navigation
    protected int nextFocus(int direction, boolean alt)
    {
        // this doesn't really override.
        return super.nextFocus(direction, alt);
    }
    
    protected void sublayout(int width, int height)
    {
        Field field;
        //get total number of fields within this manager
        int numberOfFields = getFieldCount();
        int x = 0;
        int y = 0;
        int w = 0;
        
        layoutWidth = width;
        layoutHeight = height;
        
        int pw = getPreferredWidth();
        int pad = 0;
        if (width > pw)
            pad = (width - pw) / cols;
        
        for (int j = 0; j < numberOfFields; j += cols)
        {
            int h = 0;
            x = 0;
            for (int i = j; i < numberOfFields && i < j + cols;i++)
            {
                field = getField(i); //get the field
                setPositionChild(field,x,y); //set the position for the field
                layoutChild(field, colsWidth[i-j], height); //lay out the field
                x += colsWidth[i-j] + pad;
                h = Math.max(h, field.getHeight());
            }
            w = Math.max(w,x);
            y += h;
        }
        setExtent(w, y);
    }
    
    public int getPreferredWidth()
    {
        Field field;
        //get total number of fields within this manager
        int numberOfFields = getFieldCount();

        int preferredWidth = 0;

        for (int i = 0; i < cols; i++)
            colsWidth[i] = layoutWidth / (cols);

/*        for (int i = 0; i < cols; i++)
            colsWidth[i] = 0;
            
        for (int j = 0; j < numberOfFields; j += cols)
        {
            for (int i = j; i < numberOfFields && i < j + cols;i++)
            {
                field = getField(i); //get the field
                int w = field.getPreferredWidth();
                w=40;
System.out.println("***#field width= "+w);

                colsWidth[i-j] = Math.max(colsWidth[i-j],w);
            }
        }

        int knownWidth = 0;
        int unknownCount = 0;
        
        for (int i = 0; i < cols; i++)
        {
            if (colsWidth[i] == 0x1fffffff)
            {
                unknownCount++;
            }
            else
            {
                knownWidth += colsWidth[i];
            }
        }
        
        if (unknownCount > 0)
        {
            int unknownWidth = (layoutWidth - knownWidth) / unknownCount;
            for (int i = 0; i < cols; i++)
                if (colsWidth[i] == 0x1fffffff)
                    colsWidth[i] = unknownWidth;
        }
*/        
        for (int i = 0; i < cols; i++)
            preferredWidth += colsWidth[i];
            
        return preferredWidth;
    }
    
    public int getPreferredHeight()
    {
        Field field;
        //get total number of fields within this manager
        int numberOfFields = getFieldCount();
        int preferredHeight = 0;
        for (int j = 0; j < numberOfFields; j += cols)
        {
            int h = 0;
            for (int i = j; i < numberOfFields && i < j + cols;i++)
            {
                field = getField(i); //get the field
                h = Math.max(h,field.getPreferredWidth());
            }
            preferredHeight += h;
        }
        return preferredHeight;
    }
    
    protected void paintBackground(Graphics g) {
        if (Options.getNightMode()) {
            g.setBackgroundColor(Color.BLACK);
            g.setColor(Color.RED);
        } else {
            g.setBackgroundColor(Color.WHITE);
            g.setColor(Color.BLACK);
        }
        g.clear();
    }

}
